package com.hxzy.controller.admin.dashboard.strategy;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.unit.DataUnit;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.hxzy.common.constans.RedisConstans;
import com.hxzy.controller.admin.dashboard.resp.ReportResp;
import com.hxzy.entity.Contamer;
import com.hxzy.service.ContamerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.xml.namespace.QName;
import java.util.*;

/**
 * @Author wy
 * @Description 本周客户报表
 * @Date 2023/7/17 10:28
 * @Version 1.0
 */
@Component
public class CurrentWeekReport implements ContamerReportStrategy{

    @Autowired
    private ContamerService contamerService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public ReportResp calcReport() {
        //组装
        /* 方案1范围统计，数据量小，可以这样做,表的数据量大，不建议
        select    DATE_FORMAT(create_time,'%Y-%m-%d') as createTime, count(*) as num
        from contamer
        group by DATE_FORMAT(create_time,'%Y-%m-%d')
        order by DATE_FORMAT(create_time,'%Y-%m-%d')
         */

        //方案2：循环遍历 (一般)
        //周1
//        DateTime begin = DateUtil.beginOfWeek(new Date());
//        DateTime end=DateUtil.endOfWeek(new Date());
//
//        //x坐标显示的值
//        List<String>  xAxis=new ArrayList<>();
//        List<Object> series=new ArrayList<>();
//        for(int i=0;i<7;i++){
//            DateTime dateTime = DateUtil.offsetDay(begin, i);
//            //x坐标显示的值
//            String str= DateUtil.format(dateTime,"MM/dd");
//            xAxis.add(str);
//
//            if(dateTime.isBefore(new Date())){
//                //对应的会员个数
//                Date todayBegin=DateUtil.beginOfDay(dateTime);
//                Date todayEnd=DateUtil.endOfDay(dateTime);
//
//                LambdaQueryWrapper<Contamer>  queryWrapper=new LambdaQueryWrapper<>();
//                queryWrapper.between(Contamer::getCreateTime, todayBegin,todayEnd);
//                long count = this.contamerService.count(queryWrapper);
//                series.add(count);
//            }else{
//                series.add(0);
//            }
//        }

        //方案3，使用redis缓存
       DateTime begin = DateUtil.beginOfWeek(new Date());
       DateTime end=DateUtil.endOfWeek(new Date());

       int beginDay= DateUtil.dayOfMonth(begin);
       int endDay=DateUtil.dayOfMonth(end);

        List<String>  xAxis=new ArrayList<>();
        List<Object> series=new ArrayList<>();

        //根据redis的key
        int year=DateUtil.year(new Date());
        int month=DateUtil.month(new Date())+1;
        String ym=year+""+((month<10)?"0"+month: month);
        String redisKey= RedisConstans.getRedisKey(RedisConstans.CONTAMER_HASH_KEY,ym);

        //HMGET key field1 [field2] 获取所有给定字段的值
        Collection<String> multiFields=new ArrayList<>();
        for(int i=beginDay;i<=endDay;i++){
            multiFields.add(i+"");
        }

        List<Integer> list = this.redisTemplate.opsForHash().multiGet(redisKey, multiFields);
        //组装值
        for(int i=beginDay;i<=endDay;i++){
            //时间
            DateTime dateTime = DateUtil.offsetDay(begin, i-beginDay);
            //x坐标显示的值
            String str= DateUtil.format(dateTime,"MM/dd");
            xAxis.add(str);
            //在范围之内
            if(dateTime.isBefore(new Date())) {
                Integer value = list.get(i - beginDay);
                series.add(value);
            }else{
              series.add(0);
            }
        }

        ReportResp  reportResp=new ReportResp();
        reportResp.setXAxis(xAxis);
        reportResp.setSeries(series);
        return reportResp;
    }
}
